package com.ems.service.impl.supers.action;

import com.ems.dao.supers.action.ActionDao;
import com.ems.dao.supers.action.GroupActionDao;
import com.ems.dao.supers.action.GroupDao;
import com.ems.pojo.permission.Action;
import com.ems.pojo.permission.Group;
import com.ems.pojo.permission.GroupAction;
import com.ems.service.supers.action.ActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 伟伟
 * @Date: 2019/10/5
 * @Description: 描述
 * @Version: 1.0
 */
@Service("actionService")
public class ActionServiceImpl implements ActionService {
    @Autowired
    private ActionDao actionDao;

    @Autowired
    private GroupDao groupDao;

    @Autowired
    private GroupActionDao groupActionDao;


    @Override
    public List<Action> getAllAction() {
        List<Action> parentActionList = actionDao.getAllParentAction();
        List<Action> childrenActionList = null;
        for(int i = 0;i < parentActionList.size();i ++){
            childrenActionList = actionDao.getAllChildrenActionByParentId(parentActionList.get(i).getAction_id());
            parentActionList.get(i).setList(childrenActionList);
        }
        return parentActionList;
    }

    /**
     * 修改对应的权限信息
     * @param action
     * @return
     */
    @Override
    public int updateAction(Action action) {
        return actionDao.updateAction(action);
    }


    /**
     * 获取用户对应的菜单操作权限
     * @param user_id
     * @return
     */
    @Override
    public List<Action> getPermission(int user_id) {
        //获取该用户下面可操作菜单的父权限
        List<Action> parentActionList = actionDao.getParentListAction(user_id);

        //查询用户对应的角色信息
        Group group = groupDao.getGroupByUserId(user_id);

        //在group_action表中查询指定角色下面已有的操作权限
        List<GroupAction> groupActionList = groupActionDao.findAllActionByGroupId(group.getGr_id());


        //获取对应父权限下面的所有子权限
        List<Action> childrenActionList = null;

        List<Action> list = null; //存储已有的权限

        for(int i = 0;i < parentActionList.size();i ++){

            list = new ArrayList<>();
            //利用父权限的id找出下面的所有子权限
            childrenActionList = actionDao.getChildrenListActionByParentId(parentActionList.get(i).getAction_id());

            getHaveChildrenAction(groupActionList,childrenActionList,list);

            if(list.size() > 0){
                parentActionList.get(i).setList(list);
            }

        }
        return parentActionList;
    }

    private void getHaveChildrenAction(List<GroupAction> groupActionList,List<Action> childrenActionList,List<Action> list){
        for(int  i =0;i < childrenActionList.size();i ++){
            for(int j = 0;j < groupActionList.size();j ++){
                if(childrenActionList.get(i).getAction_id() == groupActionList.get(j).getAction_id()){
                    list.add(childrenActionList.get(i));
                    break;
                }
            }
        }
    }

    /**
     * 根据action_id获取指定的权限信息
     * @param action
     * @return
     */
    @Override
    public Action getActionByActionId(Action action) {
        return actionDao.getActionByActionId(action.getAction_id());
    }


    /**
     * 根据Action_id删除对应的权限
     * @param action
     */
    @Override
    public void deleteActionById(Action action) {
        StringBuffer stringBuffer = new StringBuffer();

       //判断该权限是父权限还是子权限（根据id去查询该权限信息）
        Action action1 = actionDao.findActionById(action.getAction_id());

        stringBuffer.append(action1.getAction_id());

        //如果是父权限，则找出他所有的子权限
        if(action1.getParent_id() == 0){ //是父权限
            //根据父权限的id查询他下面所有的子权限
            List<Action> actionList = actionDao.getAllChildrenActionByParentId(action1.getAction_id());
            for(int i = 0;i < actionList.size();i ++){
                stringBuffer.append(","+actionList.get(i).getAction_id());
            }
        }
        //根据权限id删除对应的权限
        actionDao.batchDeleteActionById(stringBuffer.toString());

        //在角色与权限的关系表中删除对应的信息
        groupActionDao.batchActionByActionId(stringBuffer.toString());
    }
}
